<?php
//header("Content-Type: text/html; charset=UTF-8");
include "../config.php";
include "../util/functions.php";

$GLOBALS['PATH_TO_HOME']="../";

session_start();

checkUserSession();

$mylink = mySqlOpen($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb);

if ($_POST['publishPortfolioId']) {
    publishPortfolio($_REQUEST['publishPortfolioId'], 1);
}
if ($_POST['unpublishPortfolioId']) {
    publishPortfolio($_REQUEST['unpublishPortfolioId'], 0);
}

$page = $_REQUEST['page'];
$page = ($page) ? $page : 0;
$portfolios = getAllPortfolios($page);
$allPortfoliosCount = getAllPortfoliosCount();
$maxPageCount = $allPortfoliosCount / $GLOBALS['PORTFOLIOS_PAGE_SIZE'];
//$maxPageCount = round($maxPageCount, 0, PHP_ROUND_HALF_UP);
mySqlClose($mylink);

?>
<html>
<head>
<title>ООО "СитиМолПроект" - Современные технологии строительства и проэктирования - Портфолио</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="Современные технологии строительства и проектирования: новости, портфолио" name="description">
<meta content="строительство, проектирование, портфолио, новости" name="keywords">
<link href="../css/style.css?v=3" type="text/css" rel="stylesheet">
</head>
<body>
<center>
<table width="1004" border="0">
	<tbody><tr>
		<td colspan="4">
		</td>
	</tr>
	<tr>
		<td colspan="4">
			<table width="1004"><tbody><tr height="1"><td width="1004" bgcolor="#244aa7"></td></tr></tbody></table>
		</td>
	</tr>
	<tr>
		<td colspan="4">
            <?php
            include "../header.php";
            ?>
		</td>
	</tr>
	<tr>
		<td colspan="4">
            <?php
            include "admin_menu.php";
            ?>
		</td>
	</tr>
	<tr>
		<td style="height: 100%;" width="100">
		</td>
		<td width="900">
			<table class="main" border="0">
				<tbody>
				<tr>
					<td width="900">
						<table class="headermain">
							<tbody><tr>
								<td width="28"><img src="../img/w_small3.jpg"></td>
								<td width="416">ПОРТФОЛИО</td>
								<td width="2"></td>
							</tr>
						</tbody></table>
                        <?php
                        $cnt = 0;
                        foreach ($portfolios as $key => $portfolio) {
                            $cnt++;
                            if ($cnt > 4) {
                                //break;
                            }
                        ?>
                            <table class="occurrence" width="100%" border="0">
                                <tbody>
                                <tr height="20">
                                    <td colspan="4"><p class="date"><?php print arrayDateToWebFormDate($portfolio['date']); ?></p></td>
                                </tr>
                                <tr>
                                    <td width="60">
                            <?php   $image_file = 'upload/portfolios/img_'.$portfolio['id'].'_small.jpg';
                                    if (file_exists($GLOBALS['PATH_TO_HOME'].$image_file)) {  ?>
                                        <img src="<?php print $GLOBALS['PATH_TO_HOME'].$image_file.'?v='.$portfolio['version']; ?>" alt="<?php print htmlspecialchars($portfolio["title"]); ?>" class="occurrence">
                            <?php   } else {    ?>
                                        <img src="../img/no_news_img.jpg" alt="Фото для новости не задано"  class="occurrence">

                            <?php   }?>
                                    </td>
                                    <td width="450">
                                        <p class="more_info">
                                        <a href="portfolio_preview.php?portfolio_id=<?php print $key; ?>">
                                            <b><?php print htmlspecialchars($portfolio["title"]); ?> </b>
                                        </a>
                                        </p>
                                    </td>
                                    <td  width="130">
                                        <form action="portfolio_preview.php">
                                            <input type="hidden" name="portfolio_id" value="<?php print $portfolio["id"]; ?>">
                                            <input type="submit" value="Просмотр" class="publishbutton">
                                        </form>
                                    </td>
                                    <td  width="130">
                                        <form action="edit_portfolio.php">
                                            <input type="hidden" name="portfolioId" value="<?php print $portfolio["id"]; ?>">
                                            <input type="submit" value="Редактировать" class="publishbutton">
                                        </form>
                                    </td>
                                    <td  width="130">
                            <?php   if ($portfolio['published'] > 0) {    ?>
                                        <form method="post">
                                            <input type="hidden" name="unpublishPortfolioId" value="<?php print $portfolio["id"]; ?>">
                                            <input type="submit" value="Снять с публикации" class="unpublishbutton">
                                        </form>
                            <?php   } else {    ?>
                                        <form method="post">
                                            <input type="hidden" name="publishPortfolioId" value="<?php print $portfolio["id"]; ?>">
                                            <input type="submit" value="опубликовать" class="publishbutton">
                                        </form>
                            <?php   }   ?>
                                    </td>
                                </tr>
                            </tbody></table>
                            <br><br>
                        <?php
                        }
                        ?>
                        <table class="headermain">
                            <tbody>
                            <tr>
                                <td width="600">
                        <?php
//                        print $allPortfoliosCount.'     '.$maxPageCount ;
                            ?>
                        <?php   if ($page > 0) { ?>
                                        <a href="?page=<?php print ($page - 1); ?>">
                                            <button><< PREV <?php print $GLOBALS['PORTFOLIOS_PAGE_SIZE'] ?></button>
                                        </a>
                        <?php   }   ?>
                        <?php   if ($page < $maxPageCount - 1) { ?>
                                        <a href="?page=<?php print ($page + 1); ?>">
                                            <button>NEXT <?php print $GLOBALS['PORTFOLIOS_PAGE_SIZE'] ?> >></button>
                                        </a>
                        <?php   }   ?>

                                </td>
                            </tr>
                            </tbody>
                        </table>
                        <table class="headermain">
                            <tbody>
                            <tr>
                                <td width="600">
                                    <a href="edit_portfolio.php">
                                        <button>Добавить запись в портфолио</button>
                                    </a>
                                </td>
                            </tr>
                            </tbody>
                        </table>
					</td>
				</tr>
			</tbody></table>
		</td>
		<td width="1" bgcolor="#b2b2b2">
		</td>
		<td width="0">
		</td>

	</tr>
	<tr>
		<td colspan="4">
            <?php
            include "../footer.php";
            ?>
		</td>
	</tr>
</tbody></table>
</center>

</body></html>